package com.briup.mr;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;

/**
 * @author adam
 * @date 2023/5/31
 * 词频统计
 * 全限定类名  包名+类名
 * com.briup.mr.WordCount
 */
public class WordCount extends Configured implements Tool {

    //    定义静态内部类   实现mapper中的 map方法 自定义mapTask的业务逻辑
    public static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
        IntWritable outValue = new IntWritable(1);

        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {

//        获取读取到的一行文本内容
            String line = value.toString();
//        切割字符串 以空格作为切割点  得到字符串数组
            String[] words = line.split("[ ]");
//        遍历单词数组
            for (String word : words) {
                context.write(new Text(word), outValue);
            }

        }
    }

    public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable value : values) {
//            获取计数的int值
                sum += value.get();
            }
            context.write(key, new IntWritable(sum));

        }
    }

    @Override
    public int run(String[] args) throws Exception {
//        获取配置信息
        Configuration conf = getConf();
//        构建job
        Job job = Job.getInstance();
//        job 设置名字
        job.setJobName("自己实现的WordCount");
//        job设置运行jar包
        job.setJarByClass(this.getClass());
//        获取外部传入参数
        String in = conf.get("in");
        String out = conf.get("out");
//        通过路径字符串 创建路径对象
        Path inPath = new Path(in);
        Path outPath = new Path(out);
//        指定输入处理器  默认的是TextInPutFormat
        job.setInputFormatClass(TextInputFormat.class);
//        告诉输入处理器要处理的文件是谁
        TextInputFormat.addInputPath(job,inPath);

//        设置job的maptask要执行的代码
        job.setMapperClass(WordCountMapper.class);
//        设置map阶段的输出键值类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
//        设置reducetask个数 默认为1
        job.setNumReduceTasks(1);
//       设置job的reducetask要执行的代码
        job.setReducerClass(WordCountReducer.class);
//        设置reduce的输出的键值类型 等同于 设置最终的输出的键值类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
//设置输出处理器 默认的输出处理器是TextOutPutFormat
        job.setOutputFormatClass(TextOutputFormat.class);
//        告诉输出处理器要输出的路径
        TextOutputFormat.setOutputPath(job,outPath);
//提交job
        boolean b = job.waitForCompletion(true);
        return b?0:-1;
    }

    public static void main(String[] args) {
        try {
            ToolRunner.run(new WordCount(), args);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
